﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo ("MyTestProject")]


namespace M3G {
    public class RayIntersection {
        #region Field
        Node    intersected;
        int     submeshIndex;
        float   distance;
        float[] normal;
        float[] rayOrigin;
        float[] rayDirection;
        float[][] texture;    ///< [index][s,t]
        #endregion

        #region Method

        public RayIntersection () {
            normal = new float[3] { 0, 0, 1 };
            rayOrigin = new float[3] { 0, 0, 0 };
            rayDirection = new float[3] { 0, 0, 1 };
            intersected = null;
            submeshIndex = 0;
            distance = 0;
            texture = new float[][]{new float[]{0, 0},
                                    new float[]{0, 0}};
        }

        internal void CopyTo (RayIntersection ri) {
            Array.Copy (normal, ri.normal, 3);
            Array.Copy (rayOrigin, ri.rayOrigin, 3);
            Array.Copy (rayDirection, ri.rayDirection, 3);
            ri.intersected = intersected;
            ri.submeshIndex = submeshIndex;
            ri.distance = distance;
            Array.Copy (texture[0], ri.texture[0], 2);
            Array.Copy (texture[1], ri.texture[1], 2);
        }


        public Dictionary<String, float[]> Attribute {
            get {
                // 未実装
                return null;
            }
            internal set {
                // 未実装
            }
        }

        public float Distance {
            get { return distance; }
            internal set { distance = value; }
        }

        public Node Intersected {
            get { return intersected; }
            internal set { intersected = value; }
        }

        public float[] Normal {
            get { return (float[])normal.Clone (); }
            internal set { normal = (float[])value.Clone(); }
        }

        public float[] Ray {
            get { return rayOrigin.Concat(rayDirection).ToArray(); }
            internal set {
                rayOrigin = value.Take (3).ToArray ();
                rayDirection = value.Skip (3).Take (3).ToArray ();
            }
        }

        public int SubmeshIndex {
            get { return submeshIndex; }
            internal set { submeshIndex = value; }
        }

        public float[][] Texture {
            get {
                return texture.Select (row => row.ToArray ()).ToArray ();
            }
        }

        internal void SetTexture (int index, float[] texCoord) {
            this.texture[index] = (float[])texCoord.Clone ();
        }


        #endregion

    }
}
